# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  class << self
    
    def included( base )
      ActionController::Routing::Routes.named_routes.routes.each_key do |key|
        name = get_link_to_name( key )
        text = key.to_s.humanize.titlecase
        unless base.respond_to?( name )
          method_to_add = <<-end_eval
          
            def #{name}( *args )
              path = nil
              text = nil
              if args[0].kind_of?(String)
                text = args[0]
              else
                text = '#{text}'
                args[2] = args[1]
                args[1] = args[0]
              end
              if args[1].nil? and args[2].nil?
                path = #{key}_path
              elsif args[2].nil? and args[1] === Hash
                path = #{key}_path
              elsif !(args[1] === Hash)
                path = #{key}_path( args[1] )
              end
            
              path ||= ''
              link_to( text, path, args[2] || {} )
            end
          
          end_eval
          
          base.module_eval( method_to_add )
          
        end        
      end
    end    
    
    def get_link_to_name( key )
      "link_to_#{key}"
    end
    
  end

end
